/**
 * 我的地址 - 修改地址
 */
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class EditAddress extends StatefulWidget {
  const EditAddress({super.key});

  @override
  State<EditAddress> createState() => _EditAddressState();
}

class _EditAddressState extends State<EditAddress> {
  int? _value = 0;

  bool is_default = false;

  final List<Map<String, dynamic>> _tagList = [
    {"tag": "家", "index": 0},
    {"tag": "公司", "index": 1},
    {"tag": "学校", "index": 2},
    {"tag": "学校", "index": 3},
  ];

  @override
  Widget build(BuildContext context) {
    return BasePage(
      title: "我的地址",
      iconType: IconType.back,
      children: [
        Container(
            width: double.infinity,
            margin: EdgeInsets.all(10.w),
            child: Column(
              children: [
                //收货人的填写等区域
                SizedBox(
                  child: Card(
                    surfaceTintColor: Colors.white,
                    shadowColor: Colors.white,
                    child: Container(
                      padding: EdgeInsets.all(8.w),
                      child: Column(
                        children: [
                          //收货人
                          Container(
                            decoration: bottom_gary_border,
                            child: Row(
                              children: [
                                Text(
                                  "收货人",
                                  style: blackFont(14),
                                ),
                                SizedBox(width: 20.w),
                                Container(
                                  padding: EdgeInsets.only(top: 10.w,bottom: 10.w),
                                  width: 200.w,
                                  child: TextFormField(
                                    decoration: descript("请输入收货人姓名"),
                                  ),
                                )
                              ],
                            ),
                          ),
                          Container(
                            decoration: bottom_gary_border,
                            child: Row(
                              children: [
                                Text(
                                  "手机号码",
                                  style: blackFont(14),
                                ),
                                SizedBox(width: 20.w),
                                Container(
                                  width: 200.w,
                                  padding: EdgeInsets.only(top: 10.w,bottom: 10.w),
                                  child: TextFormField(
                                    decoration: descript("请填写收货人的手机号码"),
                                  ),
                                )
                              ],
                            ),
                          ),
                          Container(
                            decoration: bottom_gary_border,
                            child: Row(
                              children: [
                                Text(
                                  "所在地区",
                                  style: blackFont(14),
                                ),
                                SizedBox(width: 20.w),
                                Container(
                                  width: 200.w,
                                  padding: EdgeInsets.only(top: 10.w,bottom: 10.w),
                                  child: TextFormField(
                                    decoration: descript("省市区县"),
                                  ),
                                )
                              ],
                            ),
                          ),
                          SizedBox(
                            child: Row(
                              children: [
                                Text(
                                  "详细地址",
                                  style: blackFont(14),
                                ),
                                SizedBox(width: 20.w),
                                Container(
                                  padding: EdgeInsets.only(top: 10.w,bottom: 10.w),
                                  width: 200.w,
                                  child: TextFormField(
                                    decoration: descript("街道、楼牌号等"),
                                  ),
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                //标签选择及设为默认地址等
                Container(
                  margin: EdgeInsets.only(top: 20.w),
                  child: Card(
                    surfaceTintColor: Colors.white,
                    shadowColor: Colors.white,
                    child: Container(
                      padding: EdgeInsets.all(10.w),
                      child: _tagBox(),
                    ),
                  ),
                ),
              ],
            )),
        _Save()
      ],
    );
  }

  Widget _tagBox() => Container(
        child: Column(
          children: [
            //上边
            Wrap(
              crossAxisAlignment: WrapCrossAlignment.start,
              children: [
                //左边
                Container(
                  margin: EdgeInsets.only(top: 10.w),
                  width: 60.w,
                  child: Text("标签"),
                ),
                //右边
                Container(
                  // color: Colors.red,
                  child: Wrap(
                    alignment: WrapAlignment.start,
                    spacing: 8.0,
                    runSpacing: 8.0,
                    children: [
                      buildChoiceClip(),
                      //添加按钮
                      InkWell(
                        onTap: () => {},
                        child: Container(
                          decoration: border_radis(null),
                          width: 60.w,
                          height: 30.h,
                          child: Icon(Icons.add),
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
            //下边
            Container(
              // height: 80.h,
              decoration: top_gary_border,
              margin: EdgeInsets.only(top: 10.w, bottom: 10.w),
              padding: EdgeInsets.only(top: 10.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "设置默认地址",
                          style: blackFont(14),
                        ),
                        const Text("提醒：下单会优先使用该地址",
                            style: TextStyle(
                                fontSize: 12,
                                color: Color.fromRGBO(153, 153, 153, 1)))
                      ]),
                  //设为默认地址
                  Container(
                      child: Switch(
                          // activeColor: Colors.blue,
                          activeTrackColor: const Color.fromRGBO(248, 63, 45, 1),
                          value: is_default,
                          onChanged: (value) {
                            setState(() {
                              is_default = value;
                            });
                          }))
                ],
              ),
            )
          ],
        ),
      );

  /**
       * 按钮
       */
  Widget _Save() => Container(
        margin: EdgeInsets.only(top:170.w),
        width: double.infinity,
        // height: 60.h,
        color: Colors.white,
        padding: const EdgeInsets.fromLTRB(18, 18, 18, 18),
        child: ElevatedButton(
            style: ButtonStyle(
              backgroundColor:
                  MaterialStateProperty.all(const Color.fromRGBO(248, 63, 45, 1)),
            ),
            onPressed: () => {logger.d("这里是新增地址")},
            //自带的按钮有阴影，先暂时用到
            child: const Text(
              "保存",
              style: TextStyle(
                  fontSize: 14.0,
                  fontWeight: FontWeight.w500,
                  color: Colors.white),
            )),
      );

  Widget buildChoiceClip() {
    return Wrap(
      children: _tagList
          .map((e) => Padding(
                padding: EdgeInsets.only(left: 5.w, right: 5.w),
                child: buildItem(e),
              ))
          .toList(),
    );
  }

  ///构建每一个 ChoiceChip
  ChoiceChip buildItem(Map<String, dynamic> map) {
    String tag = map["tag"];
    int index = map["index"];
    return ChoiceChip(
      label: Text(tag),
      showCheckmark: false,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20.0.w), // 设置圆角
        side: BorderSide(
            color: Color.fromRGBO(243, 245, 247, 1), width: 1.0.w), // 设置边框颜色和宽度
      ),
      padding: EdgeInsets.only(left: 8.w, right: 8.w),
      selected: _value == index,
      onSelected: (bool selected) {
        setState(() {
          _value = selected ? index : null;
        });
      },
      labelStyle: TextStyle(
        color: _value == index ? Colors.white : Color.fromRGBO(51, 51, 51, 1),
      ),
      selectedColor: const Color.fromRGBO(29, 153, 255, 1),
      surfaceTintColor: Colors.white,
    );
  }
}
